package com.hh.eduorder.controller;


import com.hh.commonutils.Result;
import com.hh.commonutils.jwt.JwtUtil;
import com.hh.eduorder.entity.Order;
import com.hh.eduorder.service.OrderService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author huhuan
 * @since 2022-02-20
 */
@RestController

@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder/{courseId}")
    public Result createOrder(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String idByJwtToken = JwtUtil.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(idByJwtToken)) {
            return Result.success().data("orderId",null);
        } else {
            String orderId = orderService.createOrders(courseId, idByJwtToken);
            return Result.success().data("orderId", orderId);
        }
    }

    @GetMapping("getOrderInfo/{orderNo}")
    public Result getOrderInfo(@PathVariable("orderNo") String orderNo){
        Order byId = orderService.getOrderByOrderNo(orderNo);
        return Result.success().data("item",byId);
    }

    @GetMapping("getOrderInfoStatus/{courseId}/{memberId}")
    public int getOrderInfoStatus(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId){
        int status = orderService.getOrderInfoStatus(courseId,memberId);
        return status;
    }
}

